// miniprogram/pages/my/index.js


import {
  queryLotteryRecordCounts
} from '../../api/user.js'

const app = getApp();
const db=wx.cloud.database()
const _ = db.command;
Page({
  /**
   * 页面的初始数据
   */
  data: {     loading: true,
    aaalist:0,
    bbblist:0,
    ccclist:0,
    counts: {
      'awarded-lottery': 0,
      'total-lottery': 0,
      'waiting-lottery': 0
    },
    userInfo:null,
    shouquantype:false,
    panduanzhi:false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app.globalData.openid)
    console.log(app.globalData.userInfo)
    wx.cloud.callFunction({
      name: 'openid',
      data: {}
    }).then((res) =>{
      this .setData({
        openid:res.result.openid
      })

this.aaa(res.result.openid)
this.bbb(res.result.openid)
this.ccc(res.result.openid)

      db.collection('user').where({
        _openid: res.result.openid
      }).get().then((res) => {  
        this.setData({
          loading: false,
        })
        if (res.data.length>0) {
          app.globalData.userInfo=res.data
          this.setData({         
           userInfo:res.data[0]
          })
        }
        else{
this.setData({
  shouquantype:true
})
        }
      })
      }
    )
  },

//授权登录 
   shouquan(){
    let that=this
    // 调取哟用户信息
    wx.getUserProfile({
      desc: '用于完善会员资料',
      success: (res) => {
          console.log(res.userInfo)
          wx.cloud.database().collection('user').add({
              data: {
                  avatarUrl: res.userInfo.avatarUrl,
                  nickName: res.userInfo.nickName,    
                  jifen:100 ,
                  qiandaotype:false,
                  luckNum:10
              },
              success(res) {         
                db.collection('user').doc(res._id)
                .get().then((res) => {
                  that.setData({
                    userInfo:res.data
                  })
                  app.globalData.userInfo=res.data
                })
                  wx.showToast({
                      title: '授权成功',
                      icon: 'success',
                      duration: 1000
                  })
                  console.log('新增user ', res)                
              },
              fail(res) {
                  console.log('新增user', res)
              }
          })
      }
  })
   },
  refresh() {
    queryLotteryRecordCounts().then(res => {
      if (res.result.errMsg === "query-lottery-record-counts:ok") {
        this.setData({
          counts: res.result.data.counts
        });
      }    
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1000);
    this.refresh();
  },

  aaa(openid){

      let oldlist=[]
      db.collection('game').where({
        _openid:openid
      }).get().then((res=>{
        console.log(res)
        for(let i=0;i<res.data.length;i++){
  db.collection('active_list').where({
  _id:  res.data[i].gameXY,
  HJ: _.elemMatch({
   _openid:openid
  })
  }
  ).count().then((res=>{
  console.log(res)

      this.setData({
        aaalist:res.total
      })
  })
  )
        }
      }))
  
      },
  bbb(openid){
  
      let oldlist=[]
      db.collection('game').where({
        _openid:openid
      }).get().then((res=>{
        console.log(res)
        for(let i=0;i<res.data.length;i++){
  db.collection('active_list').where({
  _id:  res.data[i].gameXY,
  HJXY:false
  }
  ).count().then((res=>{
    console.log(res)
  
        this.setData({
          bbblist:res.total
        })
  })
  )
        }
      }))
    
  },
  ccc(openid){


      let oldlist=[]
      db.collection('game').where({
        _openid:openid
      }).get().then((res=>{
        console.log(res)
        for(let i=0;i<res.data.length;i++){
  db.collection('active_list').where({
  _id:  res.data[i].gameXY,
  
  }
  ).count().then((res=>{
    console.log(res)
  
        this.setData({
          ccclist:res.total
        })

  })
  )
        }
      }))
    
  },
  qiandao(){
  
console.log('e')
    let that=this
    if(that.data.panduanzhi==false)
{    
  that.setData({
    panduanzhi:true
  })
  wx.showLoading({
      
  })
  wx.cloud.callFunction({
      name: 'update',
      data: {
        action:'qiandao',
        _id:that.data.userInfo._id,
        jifen:that.data.userInfo.jifen+20
      }
    }).then((res) =>{
db.collection('user').where({
  _openid:that.data.openid
}).get().then((res=>{
  wx.showModal({
    title:'签到成功,增加20积分'
  })
  wx.hideLoading({
  })
  console.log(res)
  that.setData({
    userInfo:res.data[0]
  })
}))
  
    })
  }
}
})